﻿using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using SmlEngine.UI.Input;
using SmlEngine.UI.Managers;
using SmlEngine.UI.Screens;

namespace SmlEngine.UI.Controls
{
    public abstract class Control
    {
        private List<Control> children;

        public IList<Control> Children { get { return children; } }

        public virtual Rectangle Bounds { get; set; }
        public Point Location { get { return Bounds.Location; } }

        public bool IsMouseOver { get; private set; }
        public bool IsMouseDown { get; private set; }

        public Screen Screen { get; private set; }

        protected Control(Screen screen)
        {
            Screen = screen;
            children = new List<Control>();
        }

        public virtual void LoadContent(ContentManager content) { }
        public virtual void UnloadContent() { }

        public virtual void Update(GameTime gameTime, InputManager input)
        {
            IsMouseOver = Bounds.Contains(input.MouseX, input.MouseY);
            IsMouseDown = input.Current.MouseState.LeftButton == ButtonState.Pressed;

            foreach (Control c in children)
            {
                c.Update(gameTime, input);
            }
        }

        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch, Point offset)
        {
            foreach (Control c in children)
            {
                c.Draw(gameTime, spriteBatch, new Point(offset.X + Location.Y, offset.Y + Location.Y));
            }
        }
    }
}
